var view = view || {};//视角变换等一些的操作对象。
 
/*计算鼠标滑动速度的函数，保留
function startAccelera(){
	acceleraTime = setInterval("computeSpeed()",10);
	clearInterval(moveTime);
	}

function computeSpeed(){
	eyeMoveDelta["speedAngleA"] = eyeAngle[0] - eyeMoveDelta[10];
	//eyeMoveDelta["speedAngleB"] = eyeAngle[1] - eyeMoveDelta[11];
	eyeMoveDelta[10] = eyeAngle[0];
	//eyeMoveDelta[11] = eyeAngle[1];
	
	}
	
	
function stopAccelera(){
	clearInterval(acceleraTime);
	acceleraTime = null
	if(Math.abs(eyeMoveDelta["speedAngleA"])>0.05){
		moveTime = setInterval("rotAccelera()",10);
		}
	}


view.rotAccelera = function(){
	eyeAngle[0] += 	eyeMoveDelta["speedAngleA"];
	//eyeAngle[1] += 	eyeMoveDelta["speedAngleB"];
	
	eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
	//eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
	eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
	viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
													  eyePosi,
													  viewCenter,
													  [0,1,0]
													  );
	worker.updataAllLight();
	}
*/


view.moveViewCenter = function(centerAfter,tStep){
	clearInterval(moveTime);
	if (!tStep){
		eyePosi[0] = eyePosi[0] + centerAfter[0] - viewCenter[0];
		viewCenter[0] = centerAfter[0];	
		eyePosi[1] = eyePosi[1] + centerAfter[1] - viewCenter[1];
		viewCenter[1] = centerAfter[1];
		eyePosi[2] = eyePosi[2] + centerAfter[2] - viewCenter[2];
		viewCenter[2] = centerAfter[2];
		viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
															eyePosi,
															viewCenter,
															[0,1,0]
															 );
		worker.updataAllLight();
		moveTime = null;
		}
		else{
			var eyeBefore = [eyePosi[0],eyePosi[1],eyePosi[2],viewCenter[0],viewCenter[1],viewCenter[2]];
			var centerL = new Array;
			centerL[0] = centerAfter[0] - viewCenter[0];
			centerL[1] = centerAfter[1] - viewCenter[1];
			centerL[2] = centerAfter[2] - viewCenter[2];
			var n = 0;							
			moveTime = setInterval(function(){view._moveSmooth(eyeBefore,centerL,n,tStep);n++;},10);
			}
	}
	
view._moveSmooth = function(eyeBefore,centerL,n,tStep){
		eyePosi[0] = eyeBefore[0] + Math.sin(PI/2*n/tStep)*centerL[0];
		viewCenter[0] = eyeBefore[3] + Math.sin(PI/2*n/tStep)*centerL[0];	
		eyePosi[1] = eyeBefore[1] + Math.sin(PI/2*n/tStep)*centerL[1];
		viewCenter[1] = eyeBefore[4] + Math.sin(PI/2*n/tStep)*centerL[1];
		eyePosi[2] = eyeBefore[2] + Math.sin(PI/2*n/tStep)*centerL[2];
		viewCenter[2] = eyeBefore[5] + Math.sin(PI/2*n/tStep)*centerL[2];
							viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
																			  eyePosi,
																			  viewCenter,
																			  [0,1,0]
																		  );
		worker.updataAllLight();
		if (n>=tStep){
		clearInterval(moveTime);
		moveTime = null;
		}
	}
	
	
	
view.PiRound = function(rad){
	if (rad>PI){
		rad -= 2*PI;
		}
		else if(rad<=-PI){
				rad += 2*PI;
			}
		return rad;
	}

view.angleRotate = function(angleAfter,tStep){
	clearInterval(moveTime);
	if (!tStep){
				eyeAngle = angleAfter;
				eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
				eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
				eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
				viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
																  eyePosi,
																  viewCenter,
																  [0,1,0]
																  );
		worker.updataAllLight();
		moveTime = null;
		}
		else{
			var angleBefore = eyeAngle;
			var angleL = new Array;
			angleL[0] = angleAfter[0] - angleBefore[0];
			angleL[1] = angleAfter[1] - angleBefore[1];
			var n = 0;
			moveTime = setInterval(function(){angleSmooth(angleBefore,angleL,n,tStep);n++;},10);
			}
	}
	
/*保留的旋转动画函数
function angleBefore(angleBefore,angleL,n,tStep){
	eyeAngle[0] = angleBefore[0] + Math.sin(PI/2*n/tStep)*angleL[0];
	eyeAngle[1] = angleBefore[1] + Math.sin(PI/2*n/tStep)*angleL[1];
	eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
	eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
	eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
	    viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
													  eyePosi,
													  viewCenter,
													  [0,1,0]
													  );
		worker.updataAllLight();
		if (n>=tStep){
		clearInterval(moveTime);
		moveTime = null;
		}

	}	
*/

view.zoomEyeR = function(eyeRAfter,tStep){
	clearInterval(moveTime);
	if(!tStep){
		eyeR = eyeRAfter;
		eyeR = eyeR < 3 ? 3 : eyeR;
		eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
		eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
		eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
		viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
														  eyePosi,
														  viewCenter,
														  [0,1,0]
														  );
		worker.updataAllLight();
		moveTime = null;
		}
		else{
			var eyeRBefore = eyeR;
			var rL = eyeRAfter - eyeRBefore;
			var n = 0;
			moveTime = setInterval(function(){view._zoomSmooth(eyeRBefore,rL,n,tStep);n++;},10);
			}	
	}


view._zoomSmooth = function(eyeRBefore,rL,n,tStep) {
	eyeR = eyeRBefore + Math.sin(PI/2*n/tStep)*rL;
    eyeR = eyeR < 3 ? 3 : eyeR;
	eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
	eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
	eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
    viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
													  eyePosi,
													  viewCenter,
													  [0,1,0]
													  );
		worker.updataAllLight();
		if (n>=tStep){
		clearInterval(moveTime);
		mouseButton["zoomfalse"] = false;
		moveTime = null;
		}
	}

view.maxMinCoordinate = function(mass){
	var maxMinCoor = new Array;
	var returnMaxMin = new Array;
	
		for (var m=0;m < mass.length;m++){
			if (mass[m][0]){ 
							returnMaxMin = view.maxMinCoordinate(mass[m]);
							}
							else{returnMaxMin[0] = mass[m].x;
								  returnMaxMin[1] = mass[m].x;
								  returnMaxMin[2] = mass[m].y;
								  returnMaxMin[3] = mass[m].y;
								  returnMaxMin[4] = mass[m].z;
								  returnMaxMin[5] = mass[m].z;
								}
			maxMinCoor[0] = maxMinCoor[0] == undefined ? returnMaxMin[0] : Math.max(maxMinCoor[0],returnMaxMin[0]);
			maxMinCoor[1] = maxMinCoor[1] == undefined ? returnMaxMin[1] : Math.min(maxMinCoor[1],returnMaxMin[1]);
			maxMinCoor[2] = maxMinCoor[2] == undefined ? returnMaxMin[2] : Math.max(maxMinCoor[2],returnMaxMin[2]);
			maxMinCoor[3] = maxMinCoor[3] == undefined ? returnMaxMin[3] : Math.min(maxMinCoor[3],returnMaxMin[3]);
			maxMinCoor[4] = maxMinCoor[4] == undefined ? returnMaxMin[4] : Math.max(maxMinCoor[4],returnMaxMin[4]);
			maxMinCoor[5] = maxMinCoor[5] == undefined ? returnMaxMin[5] : Math.min(maxMinCoor[5],returnMaxMin[5]);
					}
	return maxMinCoor;
	}

view.resetView = function(){
	if (cubeSelect[cubeSelect.length-1][0]){
		if (!cubeDragging){
			var maxMinCoor = view.maxMinCoordinate(cubeSelect[cubeSelect.length-1]);
			var ax = maxMinCoor[0];
			var bx = maxMinCoor[1];
			var ay = maxMinCoor[2];
			var by = maxMinCoor[3];
			var az = maxMinCoor[4];
			var bz = maxMinCoor[5];	
			var targetR = Math.max( Math.max(Math.abs(ax-bx+1),Math.abs(ay-by+1)),Math.abs(az-bz+1))*2; 
			var targetX = (ax + bx)/2;
			var targetY = (ay + by)/2;
			var targetZ = (az + bz)/2;
			if (targetR < 10){targetR = 10;}
			
			var eyeTar = [eyeAngle[0],eyeAngle[1],targetR,targetX,targetY,targetZ];
			if (!eyeTarAmong){
				eyeTarAmong = [eyeAngle[0],eyeAngle[1],eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
				view.changeView(eyeTar,30);
				}
			else{
				view.changeView(eyeTarAmong,30);
				eyeTarAmong = null;
				}
			}
	}
	}
	
view.resetCentreBySele = function(){
	var seleArray = new Array;
	for(var n=0;n < cubeSelectNow.length;n++){
		seleArray[n] = cubeSelectNow[n][2];
		}
		if (!mouseButton["rotation"]){
			var maxMinCoor = view.maxMinCoordinate(seleArray);
			var ax = maxMinCoor[0];
			var bx = maxMinCoor[1];
			var ay = maxMinCoor[2];
			var by = maxMinCoor[3];
			var az = maxMinCoor[4];
			var bz = maxMinCoor[5];	
			var targetR = Math.max( Math.max(Math.abs(ax-bx+1),Math.abs(ay-by+1)),Math.abs(az-bz+1))*2; 
			var targetX = (ax + bx)/2;
			var targetY = (ay + by)/2;
			var targetZ = (az + bz)/2;
			if (targetR < 10){targetR = 10;}
			var eyeTar = [eyeAngle[0],eyeAngle[1],targetR,targetX,targetY,targetZ];
			view.changeView(eyeTar,30);
			}
	}
	
/*view.resetCentreBySele = function(){
	var seleArray = new Array;
	for(var n=0;n < cubeSelectNow.length;n++){
		seleArray[n] = cubeSelectNow[n][2];
		}
	var maxMinCoor = view.maxMinCoordinate(seleArray);
	var ax = maxMinCoor[0];
	var bx = maxMinCoor[1];
	var ay = maxMinCoor[2];
	var by = maxMinCoor[3];
	var az = maxMinCoor[4];
	var bz = maxMinCoor[5];	
	var targetX = (ax + bx)/2;
	var targetY = (ay + by)/2;
	var targetZ = (az + bz)/2;
	
	view.moveViewCenter([targetX,targetY,targetZ],20);
	}
*/	




view.resetCentre = function(mass){
	var maxMinCoor = view.maxMinCoordinate(mass);
	var ax = maxMinCoor[0];
	var bx = maxMinCoor[1];
	var ay = maxMinCoor[2];
	var by = maxMinCoor[3];
	var az = maxMinCoor[4];
	var bz = maxMinCoor[5];	
	var targetX = (ax + bx)/2;
	var targetY = (ay + by)/2;
	var targetZ = (az + bz)/2;
	view.moveViewCenter([targetX,targetY,targetZ],20);
	}
	
view.changeView = function(eyeAfter,tStep){
		clearInterval(moveTime);
		var tStep = !tStep ? 50 : tStep;
		var eyeBefore = [eyeAngle[0],eyeAngle[1],eyeR,viewCenter[0],viewCenter[1],viewCenter[2]];
		var aL = eyeAfter[0] - eyeAngle[0];
		if (aL>PI){
			aL -= 2*PI;
			}
			else if(aL<-PI){
				aL += 2*PI;
				}
		var bL = eyeAfter[1] - eyeAngle[1];
		var rL = eyeAfter[2] - eyeR;
		var xL = eyeAfter[3] - viewCenter[0];
		var yL = eyeAfter[4] - viewCenter[1];
		var zL = eyeAfter[5] - viewCenter[2];
		var n = 0;
		moveTime = setInterval(function(){view._changeEye(eyeBefore,aL,bL,rL,xL,yL,zL,n,tStep);n++;},10);
	}


view._changeEye = function(eyeBefore,aL,bL,rL,xL,yL,zL,n,tStep){
	
	
	eyeAngle[0] = eyeBefore[0] + Math.sin(PI/2*n/tStep)*aL;
	eyeAngle[1] = eyeBefore[1] + Math.sin(PI/2*n/tStep)*bL;
	eyeR = eyeBefore[2] + Math.sin(PI/2*n/tStep)*rL;
	viewCenter[0] = eyeBefore[3] + Math.sin(PI/2*n/tStep)*xL;
	viewCenter[1] = eyeBefore[4] + Math.sin(PI/2*n/tStep)*yL;
	viewCenter[2] = eyeBefore[5] + Math.sin(PI/2*n/tStep)*zL;
	
	eyePosi[2] = eyeR*Math.cos(eyeAngle[1])*Math.sin(eyeAngle[0]) + viewCenter[2];
	eyePosi[1] = eyeR*Math.sin(eyeAngle[1]) + viewCenter[1];
	eyePosi[0] = eyeR*Math.cos(eyeAngle[1])*Math.cos(eyeAngle[0]) + viewCenter[0];
	
	viewInfo.drawContext.view = o3dMath.matrix4.lookAt(
													  eyePosi,
													  viewCenter,
													  [0,1,0]
													  );
		worker.updataAllLight();
	if (n>=tStep){
		clearInterval(moveTime);
		moveTime = null;
		}
	}
	
	
	

	